//
//  NFSLoopPlayView.m
//  NeedForSpeed
//
//  Created by 王国强 on 15/10/16.
//  Copyright © 2015年 Avenger. All rights reserved.
//

#import "NFSLoopPlayView.h"

#import "BaseHeader.h"

#import "NFSLoopPlayCollectionViewCell.h"

#import "AFNETWORK.h"

//#import "NFSLoopPlayModel.h"
@implementation NFSLoopPlayView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self) {
        self =[super initWithFrame:frame];
        [self getData];
        [self creatCollectionView:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        
        
        [self createTimer];
        

    }
    return self;
}
-(void)getData
{
    [AFNETWORK getdataWithString:@"http://app.api.autohome.com.cn/autov5.0.0/mobile/appadvert-a2-pm1-v5.0.0-sid2-pid210000-cid210200-lat38.889729-lng121.551147.json" block:^(id block) {
//#warning 数据
        _loopPlayArray = [[NSMutableArray alloc]init];
        for (NSDictionary *Dic in [[block objectForKey:@"result"]objectForKey:@"list"]) {
            NFSLoopPlayModel *model = [[NFSLoopPlayModel alloc]initWithDictionary:Dic];
            [_loopPlayArray addObject:model];
        }
        _LoopPlayCollection.delegate =self;
        _LoopPlayCollection.dataSource =self;

        [_LoopPlayCollection reloadData];
    }];
}
#pragma mark - 创建轮播视图
-(void)creatCollectionView:(CGRect)frame
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    [layout setItemSize:CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT / 6)];
    [layout setMinimumInteritemSpacing:0.];
    [layout setMinimumLineSpacing:0.];
    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    [layout setSectionInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    _LoopPlayCollection = [[UICollectionView alloc]initWithFrame:frame collectionViewLayout:layout];
    [_LoopPlayCollection setContentSize:CGSizeMake(SCREEN_WIDTH *100, SCREEN_HEIGHT / 6)];
    [_LoopPlayCollection setPagingEnabled:YES];
    [self addSubview:_LoopPlayCollection];
    
    [_LoopPlayCollection registerClass:[NFSLoopPlayCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 100;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NFSLoopPlayCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if (indexPath.row % _loopPlayArray.count == 0) {
        [cell.imageView setBackgroundColor:[UIColor greenColor]];
        NFSLoopPlayModel *model = [_loopPlayArray objectAtIndex:0];
        NSURL *url = [NSURL URLWithString:model.imgurl];
        [cell.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"hoder.jpg"]];
        
    }
    if (indexPath.row %_loopPlayArray.count== 1) {
        [cell.imageView setBackgroundColor:[UIColor blueColor]];
        NFSLoopPlayModel *model = [_loopPlayArray objectAtIndex:1];
        NSURL *url = [NSURL URLWithString:model.imgurl];
        [cell.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"hoder.jpg"]];
    }
    if (indexPath.row % _loopPlayArray.count== 2) {
        [cell.imageView setBackgroundColor:[UIColor yellowColor]];
        NFSLoopPlayModel *model = [_loopPlayArray objectAtIndex:2];
        NSURL *url = [NSURL URLWithString:model.imgurl];
        [cell.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"hoder.jpg"]];
    }
    if (indexPath.row % _loopPlayArray.count == 3) {
        [cell.imageView setBackgroundColor:[UIColor purpleColor]];
        NFSLoopPlayModel *model = [_loopPlayArray objectAtIndex:3];
        NSURL *url = [NSURL URLWithString:model.imgurl];
        [cell.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"hoder.jpg"]];
    }
    if (indexPath.row % _loopPlayArray.count== 4) {
        [cell.imageView setBackgroundColor:[UIColor grayColor]];
        NFSLoopPlayModel *model = [_loopPlayArray objectAtIndex:4];
        NSURL *url = [NSURL URLWithString:model.imgurl];
        [cell.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"hoder.jpg"]];
    }
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row %_loopPlayArray.count == 0) {
        NFSLoopPlayModel *model = [_loopPlayArray objectAtIndex:0];
        [self.NFSLoopPlayDeldgate NFSLoopPlayDelegate:model];
        
    }
    if (indexPath.row % _loopPlayArray.count== 1) {
        NFSLoopPlayModel *model = [_loopPlayArray objectAtIndex:1];
        [self.NFSLoopPlayDeldgate NFSLoopPlayDelegate:model];

    }

    if (indexPath.row % _loopPlayArray.count == 2) {
        NFSLoopPlayModel *model = [_loopPlayArray objectAtIndex:2];
        [self.NFSLoopPlayDeldgate NFSLoopPlayDelegate:model];

    }

    if (indexPath.row % _loopPlayArray.count == 3) {
        NFSLoopPlayModel *model = [_loopPlayArray objectAtIndex:3];
        [self.NFSLoopPlayDeldgate NFSLoopPlayDelegate:model];

    }

    if (indexPath.row % _loopPlayArray.count == 4) {
        NFSLoopPlayModel *model = [_loopPlayArray objectAtIndex:4];
        [self.NFSLoopPlayDeldgate NFSLoopPlayDelegate:model];

    }

}
#pragma mark - 创建定时器
-(void)createTimer
{
    _loopTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerPlay) userInfo:nil repeats:YES];
    
}
-(void)timerPlay
{
    if (_LoopPlayCollection.contentOffset.x / SCREEN_WIDTH == 99) {
        [_LoopPlayCollection setContentOffset:CGPointMake(0, 0) animated:NO];
    }else
    {
    [_LoopPlayCollection setContentOffset:CGPointMake(_LoopPlayCollection.contentOffset.x + SCREEN_WIDTH, 0) animated:YES];
    }
}
@end
